<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="SpannableString与String相似，是一种字符串类型，TextView可以直接设置Spannable为显示文本，不相同的是，SpannableString可以使用setSpan方法实现字符串各种形式风格的显示，并且可以设置指定的区间。 setSpan(Object what, int start, int end, int flags)方法需要用户输入四个参数，what表示设置的格式">
<meta name="keywords" content="Android、iOS、Web、React Native">
<meta property="og:type" content="article">
<meta property="og:title" content="Android-SpannableString(显示多样式文本)">
<meta property="og:url" content="http://yoursite.com/blog/2018/08/06/Android/Android-SpannableString(显示多样式文本)/index.html">
<meta property="og:site_name" content="凌浩雨_博客">
<meta property="og:description" content="SpannableString与String相似，是一种字符串类型，TextView可以直接设置Spannable为显示文本，不相同的是，SpannableString可以使用setSpan方法实现字符串各种形式风格的显示，并且可以设置指定的区间。 setSpan(Object what, int start, int end, int flags)方法需要用户输入四个参数，what表示设置的格式">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2018-08-06T11:21:51.795Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android-SpannableString(显示多样式文本)">
<meta name="twitter:description" content="SpannableString与String相似，是一种字符串类型，TextView可以直接设置Spannable为显示文本，不相同的是，SpannableString可以使用setSpan方法实现字符串各种形式风格的显示，并且可以设置指定的区间。 setSpan(Object what, int start, int end, int flags)方法需要用户输入四个参数，what表示设置的格式">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/blog/2018/08/06/Android/Android-SpannableString(显示多样式文本)/"/>





  <title>Android-SpannableString(显示多样式文本) | 凌浩雨_博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">凌浩雨_博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/blog/blog/2018/08/06/Android/Android-SpannableString(显示多样式文本)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="凌浩雨">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="凌浩雨_博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Android-SpannableString(显示多样式文本)</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-08-06T18:01:38+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>SpannableString与String相似，是一种字符串类型，TextView可以直接设置Spannable为显示文本，不相同的是，SpannableString可以使用setSpan方法实现字符串各种形式风格的显示，并且可以设置指定的区间。</p>
<p>setSpan(Object what, int start, int end, int flags)方法需要用户输入四个参数，what表示设置的格式是什么，可以是前景色、背景色也可以是可点击的文本等等(URLSpan、ClickableSpan、BackgroundColorSpan、ForegroundColorSpan、MaskFilterSpan、AbsoluteSizeSpan、RelativeSizeSpan、ImageSpan、ScaleXSpan、StyleSpan、SubscriptSpan、SuperscriptSpan、TextAppearanceSpan、TypefaceSpan、RasterizerSpan、StrikethroughSpan、UnderlineSpan)。MaskFilterSpan可以实现模糊和浮雕效果，RasterizerSpan可以实现光栅效果。start表示需要设置格式的子字符串的起始下标，同理end表示终了下标，flags属性就有意思了，共有四种属性：</p>
<ul>
<li>Spanned.SPAN_INCLUSIVE_INCLUSIVE：前后都包括</li>
<li>Spanned.SPAN_EXCLUSIVE_EXCLUSIVE：前后都不包括</li>
<li>Spanned.SPAN_INCLUSIVE_EXCLUSIVE：前面包括，后面不包括</li>
<li>Spanned.SPAN_EXCLUSIVE_INCLUSIVE：前面不包括，后面包括<a id="more"></a>
</li>
</ul>
<p>SpannableStringBuilder实现对SpannableString的一个拼接效果，append()方法可以实现各种风格效果的SpannableString拼接，非常实用。</p>
<p>实例代码</p>
<ol>
<li><p>activity.xml文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span><br><span class="line">&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span><br><span class="line">    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</span><br><span class="line">    android:id=&quot;@+id/activity_main&quot;</span><br><span class="line">    android:layout_width=&quot;match_parent&quot;</span><br><span class="line">    android:layout_height=&quot;match_parent&quot;</span><br><span class="line">    android:orientation=&quot;vertical&quot;</span><br><span class="line">    android:layout_marginTop=&quot;10dp&quot;</span><br><span class="line">    tools:context=&quot;com.mazaiting.spannablestring.MainActivity&quot;</span><br><span class="line">    &gt;</span><br><span class="line"></span><br><span class="line">  &lt;TextView</span><br><span class="line">      android:id=&quot;@+id/textView&quot;</span><br><span class="line">      android:layout_gravity=&quot;center&quot;</span><br><span class="line">      android:layout_width=&quot;wrap_content&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:text=&quot;Hello World!&quot;</span><br><span class="line">      /&gt;</span><br><span class="line"></span><br><span class="line">  &lt;LinearLayout</span><br><span class="line">      android:layout_width=&quot;match_parent&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:orientation=&quot;horizontal&quot;</span><br><span class="line">      &gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;foreGroundColor&quot;</span><br><span class="line">        android:text=&quot;前景色&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;backGroundColor&quot;</span><br><span class="line">        android:text=&quot;背景色&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;relativeSize&quot;</span><br><span class="line">        android:text=&quot;字体大小&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;strikeThrough&quot;</span><br><span class="line">        android:text=&quot;删除线&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">  &lt;/LinearLayout&gt;</span><br><span class="line"></span><br><span class="line">  &lt;LinearLayout</span><br><span class="line">      android:layout_width=&quot;match_parent&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:orientation=&quot;horizontal&quot;</span><br><span class="line">      &gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;underLine&quot;</span><br><span class="line">        android:text=&quot;下划线&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;superScript&quot;</span><br><span class="line">        android:text=&quot;上标&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;subScript&quot;</span><br><span class="line">        android:text=&quot;下标&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;styleSpan&quot;</span><br><span class="line">        android:text=&quot;粗体斜体&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">  &lt;/LinearLayout&gt;</span><br><span class="line"></span><br><span class="line">  &lt;LinearLayout</span><br><span class="line">      android:layout_width=&quot;match_parent&quot;</span><br><span class="line">      android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">      android:orientation=&quot;horizontal&quot;</span><br><span class="line">      &gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;imageSpan&quot;</span><br><span class="line">        android:text=&quot;图片&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;click&quot;</span><br><span class="line">        android:text=&quot;点击&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line"></span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;urlSpan&quot;</span><br><span class="line">        android:text=&quot;URL&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">    &lt;Button</span><br><span class="line">        android:onClick=&quot;styleSpan&quot;</span><br><span class="line">        android:text=&quot;粗体斜体&quot;</span><br><span class="line">        android:layout_weight=&quot;1&quot;</span><br><span class="line">        android:layout_width=&quot;0dp&quot;</span><br><span class="line">        android:layout_height=&quot;wrap_content&quot;</span><br><span class="line">        /&gt;</span><br><span class="line">  &lt;/LinearLayout&gt;</span><br><span class="line">&lt;/LinearLayout&gt;</span><br></pre></td></tr></table></figure>
</li>
<li><p>MainActivity.java代码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br></pre></td><td class="code"><pre><span class="line">package com.mazaiting.spannablestring;</span><br><span class="line"></span><br><span class="line">import android.content.Intent;</span><br><span class="line">import android.graphics.Color;</span><br><span class="line">import android.graphics.Typeface;</span><br><span class="line">import android.graphics.drawable.Drawable;</span><br><span class="line">import android.support.v7.app.AppCompatActivity;</span><br><span class="line">import android.os.Bundle;</span><br><span class="line">import android.text.SpannableString;</span><br><span class="line">import android.text.Spanned;</span><br><span class="line">import android.text.TextPaint;</span><br><span class="line">import android.text.method.LinkMovementMethod;</span><br><span class="line">import android.text.style.BackgroundColorSpan;</span><br><span class="line">import android.text.style.ClickableSpan;</span><br><span class="line">import android.text.style.ForegroundColorSpan;</span><br><span class="line">import android.text.style.ImageSpan;</span><br><span class="line">import android.text.style.RelativeSizeSpan;</span><br><span class="line">import android.text.style.StrikethroughSpan;</span><br><span class="line">import android.text.style.StyleSpan;</span><br><span class="line">import android.text.style.SubscriptSpan;</span><br><span class="line">import android.text.style.SuperscriptSpan;</span><br><span class="line">import android.text.style.URLSpan;</span><br><span class="line">import android.text.style.UnderlineSpan;</span><br><span class="line">import android.util.TypedValue;</span><br><span class="line">import android.view.View;</span><br><span class="line">import android.widget.TextView;</span><br><span class="line"></span><br><span class="line">public class MainActivity extends AppCompatActivity &#123;</span><br><span class="line">  private TextView textView;</span><br><span class="line">  @Override protected void onCreate(Bundle savedInstanceState) &#123;</span><br><span class="line">    super.onCreate(savedInstanceState);</span><br><span class="line">    setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line">    textView = (TextView) this.findViewById(R.id.textView);</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置前景色</span><br><span class="line">   */</span><br><span class="line">  public void foreGroundColor(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;设置文字的前景色为淡蓝色&quot;);</span><br><span class="line">    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor(&quot;#0099EE&quot;));</span><br><span class="line">    spannableString.setSpan(foregroundColorSpan, 9, spannableString.length(),</span><br><span class="line">        Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置背景色</span><br><span class="line">   */</span><br><span class="line">  public void backGroundColor(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;设置文字的背景色为浅绿色&quot;);</span><br><span class="line">    BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.parseColor(&quot;#AC00FF30&quot;));</span><br><span class="line">    spannableString.setSpan(backgroundColorSpan, 9, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置文字大小</span><br><span class="line">   */</span><br><span class="line">  public void relativeSize(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;万丈高楼平地起&quot;);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan1 = new RelativeSizeSpan(1.2f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan2 = new RelativeSizeSpan(1.4f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan3 = new RelativeSizeSpan(1.6f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan4 = new RelativeSizeSpan(1.8f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan5 = new RelativeSizeSpan(1.6f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan6 = new RelativeSizeSpan(1.4f);</span><br><span class="line">    RelativeSizeSpan relativeSizeSpan7 = new RelativeSizeSpan(1.2f);</span><br><span class="line"></span><br><span class="line">    spannableString.setSpan(relativeSizeSpan1, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan2, 1, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan3, 2, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan4, 3, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan5, 4, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan6, 5, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line">    spannableString.setSpan(relativeSizeSpan7, 6, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);</span><br><span class="line"></span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置删除线</span><br><span class="line">   */</span><br><span class="line">  public void strikeThrough(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置删除线&quot;);</span><br><span class="line">    StrikethroughSpan strikethroughSpan = new StrikethroughSpan();</span><br><span class="line">    spannableString.setSpan(strikethroughSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置下划线</span><br><span class="line">   */</span><br><span class="line">  public void underLine(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置下划线&quot;);</span><br><span class="line">    UnderlineSpan underlineSpan = new UnderlineSpan();</span><br><span class="line">    spannableString.setSpan(underlineSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置上标</span><br><span class="line">   */</span><br><span class="line">  public void superScript(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置上标&quot;);</span><br><span class="line">    SuperscriptSpan superscriptSpan = new SuperscriptSpan();</span><br><span class="line">    spannableString.setSpan(superscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置下标</span><br><span class="line">   */</span><br><span class="line">  public void subScript(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置下标&quot;);</span><br><span class="line">    SubscriptSpan subscriptSpan = new SubscriptSpan();</span><br><span class="line">    spannableString.setSpan(subscriptSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置粗体斜体</span><br><span class="line">   */</span><br><span class="line">  public void styleSpan(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置粗体、斜体风格&quot;);</span><br><span class="line">    StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);</span><br><span class="line">    StyleSpan styleSpan_I = new StyleSpan(Typeface.ITALIC);</span><br><span class="line">    spannableString.setSpan(styleSpan_B, 5, 7, Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    spannableString.setSpan(styleSpan_I, 8, 10, Spanned.SPAN_INCLUSIVE_INCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 设置图片</span><br><span class="line">   */</span><br><span class="line">  public void imageSpan(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;在文本中添加表情(表情)&quot;);</span><br><span class="line">    Drawable drawable = getResources().getDrawable(R.mipmap.emotion);</span><br><span class="line">    drawable.setBounds(0, 0, 42, 42);</span><br><span class="line">    ImageSpan imageSpan = new ImageSpan(drawable);</span><br><span class="line">    spannableString.setSpan(imageSpan, 6, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 点击开启新界面</span><br><span class="line">   */</span><br><span class="line">  public void click(View view)&#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置点击事件&quot;);</span><br><span class="line">    MyClickableSpan clickableSpan = new MyClickableSpan(&quot;http://www.jianshu.com/&quot;);</span><br><span class="line">    spannableString.setSpan(clickableSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);</span><br><span class="line">    // 使用ClickableSpan的文本如果想真正实现点击作用，</span><br><span class="line">    // 必须为TextView设置setMovementMethod方法，否则没有点击相应，</span><br><span class="line">    // 至于setHighlightColor方法则是控制点击是的背景色。</span><br><span class="line">    textView.setMovementMethod(LinkMovementMethod.getInstance());</span><br><span class="line">    textView.setHighlightColor(Color.parseColor(&quot;#36969696&quot;));</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  class MyClickableSpan extends ClickableSpan &#123;</span><br><span class="line"></span><br><span class="line">    private String content;</span><br><span class="line"></span><br><span class="line">    public MyClickableSpan(String content) &#123;</span><br><span class="line">      this.content = content;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void updateDrawState(TextPaint ds) &#123;</span><br><span class="line">      ds.setUnderlineText(false);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override public void onClick(View widget) &#123;</span><br><span class="line">      Intent intent = new Intent(MainActivity.this, SecondActivity.class);</span><br><span class="line">      Bundle bundle = new Bundle();</span><br><span class="line">      bundle.putString(&quot;content&quot;, content);</span><br><span class="line">      intent.putExtras(bundle);</span><br><span class="line">      startActivity(intent);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  /**</span><br><span class="line">   * 网络链接</span><br><span class="line">   */</span><br><span class="line">  public void urlSpan(View view) &#123;</span><br><span class="line">    SpannableString spannableString = new SpannableString(&quot;为文字设置超链接&quot;);</span><br><span class="line">    URLSpan urlSpan = new URLSpan(&quot;http://www.jianshu.com/&quot;);</span><br><span class="line">    spannableString.setSpan(urlSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);</span><br><span class="line">    textView.setMovementMethod(LinkMovementMethod.getInstance());</span><br><span class="line">    textView.setHighlightColor(Color.parseColor(&quot;#36969696&quot;));</span><br><span class="line">    textView.setText(spannableString);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>注: 文章中涉及到的Activity即一个空的Activity, 读者可以可以新建一个空界面即可。</p>
<p><a href="https://gitee.com/zaitingma/codes/bq3on2i6cls5w9thzy8uj72" target="_blank" rel="noopener">代码下载</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-RecyclerView使用/" rel="next" title="Android-RecyclerView使用">
                <i class="fa fa-chevron-left"></i> Android-RecyclerView使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/08/06/Android/Android-SparseArray与ArrayMap/" rel="prev" title="Android-SparseArray与ArrayMap">
                Android-SparseArray与ArrayMap <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/blog/images/avatar.jpeg"
                alt="凌浩雨" />
            
              <p class="site-author-name" itemprop="name">凌浩雨</p>
              <p class="site-description motion-element" itemprop="description">专业从事于移动端开发，Android与iOS均有涉及，主做Android开发与React Native开发</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">393</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">凌浩雨</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
